FROM phusion/baseimage:0.11 AS base

# https://opsx.alibaba.com/mirror ubuntu 18.04
COPY etc/apt-sources.list /etc/apt/sources.list

RUN apt-get update && \
    apt-get install -y python3-venv python3-pip && \
    rm -rf /var/lib/apt/lists/* && \
    ln -s /usr/bin/python3 /usr/bin/python && \
    ln -s /usr/bin/pip3 /usr/bin/pip

RUN apt-get update && \
    apt-get install -y git openssl openssh-client vim curl wget htop \
    tree shellcheck && \
    rm -rf /var/lib/apt/lists/*

RUN mkdir -p /app
WORKDIR /app

ARG PYPI_MIRROR="https://mirrors.aliyun.com/pypi/simple/"
ENV PIP_INDEX_URL=$PYPI_MIRROR PIP_DISABLE_PIP_VERSION_CHECK=1

###############################################################################

FROM base AS build

COPY requirements.txt .
RUN python -m venv /app/.venv && \
    /app/.venv/bin/pip install 'pip==21.1.2' && \
    /app/.venv/bin/pip install -r requirements.txt && \
    rm requirements.txt

COPY . .
RUN /app/.venv/bin/python setup.py sdist && \
    /app/.venv/bin/pip install "dist/$(ls dist/)" && \
    /app/.venv/bin/tinyops --help

###############################################################################

FROM base

COPY --from=build /app/.venv /app/.venv
RUN ln -s /app/.venv/bin/tinyops /bin/tinyops

EXPOSE 9010 9011
VOLUME [ "/app/conf", "/app/data", "/app/temp", "/app/logs", "/var/run/docker.sock" ]
ENV TINYOPS_CONFIG /app/conf/tinyops.toml

CMD [ "/sbin/my_init", "--", "/bin/tinyops", "start" ]
